package com.ludaxu.zhbj.base.menudetail;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import cn.sharesdk.evernote.n;
import cn.sharesdk.framework.h;

import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.ludaxu.zhbj.R;
import com.ludaxu.zhbj.base.BaseMenuDetailPager;
import com.ludaxu.zhbj.domain.PhotoData;
import com.ludaxu.zhbj.domain.PhotoData.PhotoInfo;
import com.ludaxu.zhbj.global.GlobalContants;
import com.ludaxu.zhbj.utils.CacheUtils;
import com.ludaxu.zhbj.utils.bitmap.MyBitmapUtils;

/**
 * 菜单详情页-组图
 * 
 * @author ludaxu
 * 
 */
public class PhotoMenuDetailPager extends BaseMenuDetailPager {

	private ListView lvPhoto;
	private GridView gvPhoto;
	private ArrayList<PhotoInfo> mPhotoList;
	private PhotoAdapter mAdapter;
	ImageView btnPhoto;

	/**
	 * 实现父类的构造方法
	 * 
	 * @param activity
	 * @param btnPhoto
	 */
	public PhotoMenuDetailPager(Activity activity, ImageView btnPhoto) {
		super(activity);
		this.btnPhoto = btnPhoto;
		btnPhoto.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changDisplay();
			}
		});
	}

	private boolean isListDisplay = true;// 默认时列表展示

	/**
	 * 切换展现方式
	 */
	protected void changDisplay() {
		if (isListDisplay) {
			isListDisplay = false;
			lvPhoto.setVisibility(View.GONE);
			gvPhoto.setVisibility(View.VISIBLE);
			btnPhoto.setImageResource(R.drawable.icon_pic_list_type);
		} else {
			isListDisplay = true;
			lvPhoto.setVisibility(View.VISIBLE);
			gvPhoto.setVisibility(View.GONE);
			btnPhoto.setImageResource(R.drawable.icon_pic_grid_type);
		}
	}

	@Override
	public View initViews() {
		View view = View.inflate(mActivity, R.layout.menu_photo_pager, null);
		lvPhoto = (ListView) view.findViewById(R.id.lv_photo);
		gvPhoto = (GridView) view.findViewById(R.id.gv_photo);

		return view;
	}

	@Override
	public void initData() {
		String cache = CacheUtils
				.getCache(mActivity, GlobalContants.PHOTOS_URL);
		if (!TextUtils.isEmpty(cache)) {

		}
		getDataFromServer();

	}

	private void getDataFromServer() {
		HttpUtils utils = new HttpUtils();
		utils.send(HttpMethod.GET, GlobalContants.PHOTOS_URL,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						String result = responseInfo.result;
						parseData(result);
						// 设置缓存
						CacheUtils.setCache(mActivity,
								GlobalContants.PHOTOS_URL, result);

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						Toast.makeText(mActivity, "onFailure", 0).show();

					}
				});
	}

	protected void parseData(String result) {
		Gson gson = new Gson();
		PhotoData data = gson.fromJson(result, PhotoData.class);
		mPhotoList = data.data.news;// 获取组图列表集合
		if (mPhotoList != null) {
			mAdapter = new PhotoAdapter();
			lvPhoto.setAdapter(mAdapter);
			gvPhoto.setAdapter(mAdapter);
		}

	}

	class PhotoAdapter extends BaseAdapter {
		// 这个时xUtils的工具
		// private BitmapUtils utils;
		// 用自定义的图片加载工具
		private MyBitmapUtils utils;

		public PhotoAdapter() {
			// utils = new BitmapUtils(mActivity);
			// utils.configDefaultLoadFailedImage(R.drawable.pic_item_list_default);
			utils = new MyBitmapUtils();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mPhotoList.size();
		}

		@Override
		public PhotoInfo getItem(int position) {
			// TODO Auto-generated method stub
			return mPhotoList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = View.inflate(mActivity, R.layout.list_photo_item,
						null);
				holder = new ViewHolder();
				holder.ivPic = (ImageView) convertView
						.findViewById(R.id.iv_pic);
				holder.tvTitle = (TextView) convertView
						.findViewById(R.id.tv_title);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			PhotoInfo item = getItem(position);
			holder.tvTitle.setText(item.title);

			utils.display(holder.ivPic, item.listimage);

			return convertView;
		}
	}

	static class ViewHolder {
		ImageView ivPic;
		TextView tvTitle;
	}

}
